<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

use think\facade\Route;
use think\facade\Db;

// 参数
$lang = app('http')->getName();
$url_category = config('global.url.category'); $url_view = config('global.url.view');
$url_root = config('global.url.root'); $url_root_module = config('global.url.root_module');
// 模型
$moduleWhere[] = ['lang','=',$lang]; $moduleWhere[] = ['disabled','=',1];
$moduleList = Db::name('module')->where($moduleWhere)->field('module,tabledir,urlroute')->select();

foreach ($moduleList as $rs) {
	$module = $rs['module']; $tabledir = $rs['tabledir']; $urlroute = $rs['urlroute'];
	// 模块首页
	Route::get("/".$urlroute."/$", $tabledir."/index");
	// 列表页
	Route::get("/".$urlroute."/:page/$", $tabledir."/index")->pattern(['page' => '\d+']);

	/* 分类页 开始 */
	$sortWhere[] = ['lang','=',$lang]; $sortWhere[] = ['signid','=',1];
	switch ($url_category) {
		case 0:
			$sortList = Db::name('sort')->where($sortWhere)->where('tabledir',$tabledir)->field('id,urlroute')->select();
			foreach ($sortList as $sortRs) {
				Route::get("/".$sortRs['urlroute']."/", $tabledir."/index")->append(['sortid' => $sortRs['id']]);
				Route::get("/".$sortRs['urlroute']."/:page/$", $tabledir."/index")->append(['sortid' => $sortRs['id']])->pattern(['page' => '\d+']);
			}
			break;
		case 1:
			// 一级分类
			$fistSort = Db::name('sort')->where($sortWhere)->where('tabledir',$tabledir)->where('parentid',0)->field('id,urlroute')->select();
			foreach ($fistSort as $oneRs) {
				// 二级分类
				$secondSort = Db::name('sort')->where($sortWhere)->where('tabledir',$tabledir)->where('parentid',$oneRs['id'])->field('id,urlroute')->select();
				foreach ($secondSort as $twoRs) {
					// 三级分类
					$thirdSort = Db::name('sort')->where($sortWhere)->where('tabledir',$tabledir)->where('parentid',$twoRs['id'])->field('id,urlroute')->select();
					foreach ($thirdSort as $threeRs) {
						// 四级分类
						$fourthSort = Db::name('sort')->where($sortWhere)->where('tabledir',$tabledir)->where('parentid',$threeRs['id'])->field('id,urlroute')->select();
						foreach ($fourthSort as $fourRs) {
							Route::get("/".$oneRs['urlroute']."/".$twoRs['urlroute']."/".$threeRs['urlroute']."/".$fourRs['urlroute']."/", $tabledir."/index")->append(['sortid' => $fourRs['id']]);
							Route::get("/".$oneRs['urlroute']."/".$twoRs['urlroute']."/".$threeRs['urlroute']."/".$fourRs['urlroute']."/:page/$", $tabledir."/index")->append(['sortid' => $fourRs['id']])->pattern(['page' => '\d+']);
						}
						Route::get("/".$oneRs['urlroute']."/".$twoRs['urlroute']."/".$threeRs['urlroute']."/", $tabledir."/index")->append(['sortid' => $threeRs['id']]);
						Route::get("/".$oneRs['urlroute']."/".$twoRs['urlroute']."/".$threeRs['urlroute']."/:page/$", $tabledir."/index")->append(['sortid' => $threeRs['id']])->pattern(['page' => '\d+']);
					}
					Route::get("/".$oneRs['urlroute']."/".$twoRs['urlroute']."/", $tabledir."/index")->append(['sortid' => $twoRs['id']]);
					Route::get("/".$oneRs['urlroute']."/".$twoRs['urlroute']."/:page/$", $tabledir."/index")->append(['sortid' => $twoRs['id']])->pattern(['page' => '\d+']);
				}
				Route::get("/".$oneRs['urlroute']."/", $tabledir."/index")->append(['sortid' => $oneRs['id']]);
				Route::get("/".$oneRs['urlroute']."/:page/$", $tabledir."/index")->append(['sortid' => $oneRs['id']])->pattern(['page' => '\d+']);
			}
			break;
		case 2:
			$sortList = Db::name('sort')->where($sortWhere)->where('tabledir',$tabledir)->field('id,urlroute')->select();
			foreach ($sortList as $sortRs) {
				Route::get('/'.$urlroute."/".$sortRs['urlroute']."/", $tabledir."/index")->append(['sortid' => $sortRs['id']]);
				Route::get('/'.$urlroute."/".$sortRs['urlroute']."/:page/$", $tabledir."/index")->append(['sortid' => $sortRs['id']])->pattern(['page' => '\d+']);
			}
			break;
		case 3:
			// 一级分类
			$fistSort = Db::name('sort')->where($sortWhere)->where('tabledir',$tabledir)->where('parentid',0)->field('id,urlroute')->select();
			foreach ($fistSort as $oneRs) {
				// 二级分类
				$secondSort = Db::name('sort')->where($sortWhere)->where('tabledir',$tabledir)->where('parentid',$oneRs['id'])->field('id,urlroute')->select();
				foreach ($secondSort as $twoRs) {
					// 三级分类
					$thirdSort = Db::name('sort')->where($sortWhere)->where('tabledir',$tabledir)->where('parentid',$twoRs['id'])->field('id,urlroute')->select();
					foreach ($thirdSort as $threeRs) {
						// 四级分类
						$fourthSort = Db::name('sort')->where($sortWhere)->where('tabledir',$tabledir)->where('parentid',$threeRs['id'])->field('id,urlroute')->select();
						foreach ($fourthSort as $fourRs) {
							Route::get('/'.$urlroute."/".$oneRs['urlroute']."/".$twoRs['urlroute']."/".$threeRs['urlroute']."/".$fourRs['urlroute']."/", $tabledir."/index")->append(['sortid' => $fourRs['id']]);
							Route::get('/'.$urlroute."/".$oneRs['urlroute']."/".$twoRs['urlroute']."/".$threeRs['urlroute']."/".$fourRs['urlroute']."/:page/$", $tabledir."/index")->append(['sortid' => $fourRs['id']])->pattern(['page' => '\d+']);
						}
						Route::get('/'.$urlroute."/".$oneRs['urlroute']."/".$twoRs['urlroute']."/".$threeRs['urlroute']."/", $tabledir."/index")->append(['sortid' => $threeRs['id']]);
						Route::get('/'.$urlroute."/".$oneRs['urlroute']."/".$twoRs['urlroute']."/".$threeRs['urlroute']."/:page/$", $tabledir."/index")->append(['sortid' => $threeRs['id']])->pattern(['page' => '\d+']);
					}
					Route::get('/'.$urlroute."/".$oneRs['urlroute']."/".$twoRs['urlroute']."/", $tabledir."/index")->append(['sortid' => $twoRs['id']]);
					Route::get('/'.$urlroute."/".$oneRs['urlroute']."/".$twoRs['urlroute']."/:page/$", $tabledir."/index")->append(['sortid' => $twoRs['id']])->pattern(['page' => '\d+']);
				}
				Route::get('/'.$urlroute."/".$oneRs['urlroute']."/", $tabledir."/index")->append(['sortid' => $oneRs['id']]);
				Route::get('/'.$urlroute."/".$oneRs['urlroute']."/:page/$", $tabledir."/index")->append(['sortid' => $oneRs['id']])->pattern(['page' => '\d+']);
			}
			break;
	}
	/* 分类页 结束 */

	/* 详情页 开始 */
	/* 禁用：内容页置于根目录，或：启用但不是当前模型 */
	if($url_root == 0 or ($url_root == 1 and $url_root_module != $tabledir)){
		switch ($url_view) {
			case 0:
				Route::get('/'.$urlroute.'/:urlroute$',$tabledir.'/view')->pattern(['urlroute' => '[A-Za-z0-9\-]+']);
				break;
			case 1:
				Route::get('/'.$urlroute.'/:id$',$tabledir.'/view')->pattern(['id' => '\d+']);
				break;
		}
	}
	/* 启用：内容页置于根目录，且是当前模型*/
	if($url_root == 1 and $url_root_module == $tabledir){
		switch ($url_view) {
			case 0:
				Route::get('/:urlroute$',$tabledir.'/view')->pattern(['urlroute' => '[A-Za-z0-9\-]+']);
				break;
			case 1:
				Route::get('/:id$',$tabledir.'/view')->pattern(['id' => '\d+']);
				break;
		}
	}
	/* 详情页 结束 */

	// 下载文件
	Route::get('/'.$tabledir.'/file/:id$',$tabledir.'/download')->pattern(['id' => '\d+']);
}

// 在线询价
Route::get('/feedback/consult$','feedback/consult')->pattern(['tabledir' => '[A-Za-z0-9\-]+','id' => '\d+']);
// 关键词
Route::get('/tags/:urlroute/:page/$','site/tags')->pattern(['urlroute' => '[A-Za-z0-9\-]+','page' => '\d+']);
Route::get('/tags/:urlroute/','site/tags')->pattern(['urlroute' => '[A-Za-z0-9\-]+']);
// 验证码
Route::get('captcha/[:config]','\\think\\captcha\\CaptchaController@index');

// 启用中间件
Route::group(function(){
	Route::rule('user/index','user/index');
})->middleware(\app\middleware\Auth::class);
